﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_buy_orderOn : System.Web.UI.Page
{
    public Tuan.Model.OrderInfo orderInfo = new Tuan.Model.OrderInfo();
    public Tuan.Model.UserInfo userInfo = new Tuan.Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        string oid = Request["oid"];
        if (Request["oid"] != null)
        {
            orderInfo = (new Tuan.BLL.OrderInfo()).GetWaitPayModel(int.Parse(oid));
            if (orderInfo == null)
            {
                orderInfo = new Tuan.Model.OrderInfo();
                Response.Write("<script type='text/javascript'>alert('该订单不能操作，可能的原因有：①该订单不存在！②该订单已经下架！③该订单已被处理');window.location='/pages/usercenter/myorderlist.aspx';</script>");
                //Pan.Common.MessageBox.ShowAndRedirect(this, "该订单不能操作，可能的原因有：①该订单不存在！②该订单已经下架！③该订单已被处理", "/pages/usercenter/index.aspx");
            }
            else
            {
                if (orderInfo.PayMoney <= 0)
                {
                    DateTime paydate = DateTime.Now;
                    int flag = (new Tuan.BLL.OrderInfo()).Update(orderInfo.OrderNum,"0", "7", "","", paydate);
                    if (flag > 0)
                        Pan.Log.WriteLog(string.Format("数据库操作;订单号:{0},时间:{1} 数据库写入成功",orderInfo.OrderNum,DateTime.Now.ToString()), "UnNeedPay");
                    else
                        Pan.Log.WriteLog(string.Format("数据库操作;订单号:{0},失败时间:{1},通知重发，已作数据库处理，返回行数:{2}","不需要支付", DateTime.Now.ToString(), flag), "UnNeedPay");
                    Response.Write("<script type='text/javascript'>alert('该订单交易成功');window.location='/pages/usercenter/myorderlist.aspx';</script>");
                }
                userInfo = (new Tuan.BLL.UserInfo()).GetModel(User.Identity.Name);
                //if (userInfo.TokenAmount < orderInfo.Token)
                //{
                //    orderInfo.Token = userInfo.TokenAmount;
                //    orderInfo.PayMoney = orderInfo.TotalAmount - orderInfo.Token;
                //    (new Tuan.BLL.OrderInfo()).Update(orderInfo.OrderID, orderInfo.PayMoney, orderInfo.Token);
                //}
            }
        }
        else
        {
            Response.Write("<script type='text/javascript'>window.location='/pages/usercenter/MyOrderList.aspx';</script>");
        }
    }
}
